package com.dap.servicegovernance.trace;

import java.util.ArrayList;
import java.util.List;

/**
 * 叶子节点结构
 * 
 * @author wshh-zhaobin
 *
 */
public class RpcTreeNode {

    private String nodeId;

    private String parentId;

    private String text;

    // 子节点list
    protected List<RpcTreeNode> childList;


    public RpcTreeNode(String nodeId, String parentId) {
        this.setNodeId(nodeId);
        this.setParentId(parentId);
        // this.setText(text);

        childList = new ArrayList<>();
    }


    public void addChild(RpcTreeNode child) {
        if (this.nodeId.equals(child.getParentId())) {
            this.getChildList().add(child);
        } else {

            for (RpcTreeNode item : this.getChildList()) {
                if (item.getNodeId().equals(child.getParentId())) {
                    item.getChildList().add(child);
                    break;
                } else {
                    if (item.getChildList() != null && item.getChildList().size() > 0) {
                        item.addChild(child);
                    }

                }
            }
        }
    }


    public RpcTreeNode findNodeById(String nodeId) {

        if (this.nodeId.equals(nodeId))
            return this;
        if (childList == null || childList.size() <= 0) {
            return null;
        } else {
            for (RpcTreeNode item : childList) {

                RpcTreeNode resultNode = item.findNodeById(nodeId);

                if (resultNode != null) {
                    return resultNode;
                }
            }
            return null;
        }

    }


    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getParentId() {
        return parentId;
    }

    public void setParentId(String parentId) {
        this.parentId = parentId;
    }

    public String getNodeId() {
        return nodeId;
    }

    public void setNodeId(String nodeId) {
        this.nodeId = nodeId;
    }

    public List<RpcTreeNode> getChildList() {
        return childList;
    }

    public void setChildList(List<RpcTreeNode> childlist) {
        this.childList = childlist;
    }

}